<template>
	<view>
		<!-- 顶部导航 -->
		<u-navbar :background="background">
			<view class="ft_16 fw-b">
				我的二维码
			</view>
		</u-navbar>
		<view class="content" style="padding: 20px 15px;">
			
			<view class="con_2">
				<view class="p_bottom_10 ft_16 c-333 fw-b">
					我的二维码
				</view>
				<view class="">
					<!-- #ifdef APP-PLUS || H5 -->
					<image :src="codePath" @longpress="savePhoto(codePath)"></image>
					<!-- #endif -->
					<!-- #ifdef MP -->
					<image :src="codePath" @longpress="savePosterPath(codePath)"></image>
					<!-- #endif -->
					
				</view>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background:{backgroundColor: '#ffffff',}, //导航栏背景
				codePath:"", //推广码
				
			}
		},
		onLoad() {
			this.getEWM() //获取二维码
		},
		methods: {
			getEWM(){ //获取二维码
				this.$myRequest('qrcode/getCode',{})
				.then(res=>{
					console.log(res.data);
					this.codePath=res.data.codePath //推广码
				})
			},
			
			savePhoto(url) { //app长按保存
				return
				// 先下载图片
				uni.downloadFile({url,
					success: (res) => {
						console.log(res);
						return
						// 获取到图片本地地址后再保存图片到相册（因为此方法不支持远程地址）
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: () => {
								uni.showToast({
									title: "保存成功！",
								});
							},
							fail: () => {
								uni.showToast({
									title: "保存失败",
								});
							},
						});
					},
				});
			},
			// 小程序保存图片
			savePosterPath(url) {
				return
				uni.downloadFile({
					url,
					success: (resFile) => {
						console.log(resFile, "resFile");
						if (resFile.statusCode === 200) {
							uni.getSetting({
								success: (res) => {
									if (!res.authSetting["scope.writePhotosAlbum"]) {
										uni.authorize({
											scope: "scope.writePhotosAlbum",
											success: () => {
												uni.saveImageToPhotosAlbum({
													filePath: resFile.tempFilePath,
													success: (res) => {
														return uni.showToast({
															title: "保存成功！",
														});
													},
													fail: (res) => {
														return uni.showToast({
															title: res.errMsg,
														});
													},
													complete: (res) => {},
												});
											},
											fail: () => {
												uni.showModal({
													title: "您已拒绝获取相册权限",
													content: "是否进入权限管理，调整授权？",
													success: (res) => {
														if (res.confirm) {
															uni.openSetting({
																success: (res) => {
																	console.log(res.authSetting);
																},
															});
														} else if (res.cancel) {
															return uni.showToast({
																title: "已取消！",
															});
														}
													},
												});
											},
										});
									} else {
										uni.saveImageToPhotosAlbum({
											filePath: resFile.tempFilePath,
											success: (res) => {
												return uni.showToast({
													title: "保存成功！",
												});
											},
											fail: (res) => {
												return uni.showToast({
													title: res.errMsg,
												});
											},
											complete: (res) => {},
										});
									}
								},
								fail: (res) => {},
							});
						} else {
							return uni.showToast({
								title: resFile.errMsg,
							});
						}
					},
					fail: (res) => {
						return uni.showToast({
							title: res.errMsg,
						});
					},
				});
			},
			
		}
	}
</script>

<style lang="scss">
page{
	background-color: #fafafa;
}
.content{
	.con_2{
		text-align: center;padding: 100px 0;
		image{
			width: 220px;
			height: 220px;
		}
	}
}

</style>
